package com.augurit.gis.edit;

import java.io.IOException;
import com.augurit.gis.server.ServerContextFactory;
import com.esri.arcgis.carto.IFeatureLayer;
import com.esri.arcgis.carto.IFeatureLayerProxy;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.carto.MapServer;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.IFeatureClass;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.server.IServerContext;

public class EditUtil extends EditService {
	/**
	 * 添加点的方法
	 * @param x
	 * @param y
	 * @param serverName
	 * @param serverType
	 * @param layerId
	 * @param strkeyValues
	 * @return
	 * @throws AutomationException
	 * @throws IOException
	 */
	 public String addPoint(Double x,Double y,String serverName,String serverType,int layerId ,String strkeyValues) throws AutomationException,IOException
	 {
	  IServerContext context = null;
      try
      {
    	     context =ServerContextFactory.getContext(serverName, serverType);
			  MapServer mapServer = (MapServer) context.getServerObject();
			  ILayer editLayer = mapServer.getLayer("",layerId);
			  System.out.println("编辑的图层名称:"+editLayer.getName());
			  IFeatureLayer featureLayer = new IFeatureLayerProxy(editLayer);
			  IFeatureClass featureClass = featureLayer.getFeatureClass();
			  IFeature feature = featureClass.createFeature();
			  IFields fields = feature.getFields(); 
			  IPoint point = (IPoint)context.createObject(Point.getClsid()); 
			  point.setX(x);
			  point.setY(y);
			  //strkeyValues的字符串格式为:列名1=值1,列名2=值2,.....
			  String []keyValues=strkeyValues.split(",");
			  for(String strkeyValue:keyValues){
				  String [] keyValue=strkeyValue.split("=");
				  feature.setValue(fields.findField( keyValue[0]),  keyValue[1]);
			  }
			  feature.setShapeByRef(point);
			  feature.store();  
	    	  context.releaseContext();
	    	  return EditResult.Succeed;
      }
      catch(Exception ee)
      {
    	  ee.printStackTrace();
    	  if(context!=null)
    	  {
    	    context.releaseContext();
    	  }
    	  return EditResult.Failed;
      }
	}
/**
 * 添加线的方法
 * @param pointListString
 * @param serverName
 * @param serverType
 * @param layerId
 * @param strkeyValues
 * @return
 * @throws AutomationException
 * @throws IOException
 */
public String  addPolyline(String pointListString,String serverName,String serverType,int layerId,String strkeyValues) throws AutomationException,IOException
{
	 String[] pointArr=pointListString.split(",");
 IServerContext context = null;
 try
 {
	      context =ServerContextFactory.getContext(serverName, serverType);
		  MapServer mapServer = (MapServer) context.getServerObject();
		  ILayer editLayer = mapServer.getLayer("",layerId);
		  System.out.println("编辑的图层名称:"+editLayer.getName());
		  IFeatureLayer featureLayer = new IFeatureLayerProxy(editLayer);
		  IFeatureClass featureClass = featureLayer.getFeatureClass();
		  IFeature feature = featureClass.createFeature();
		  IFields fields = feature.getFields(); 
		  Polyline p=(Polyline)context.createObject(Polyline.getClsid());
	      for  ( int  i =  0 ; i < pointArr.length; i+=2)    
	      {    
	            IPoint pPoint;     
	            pPoint = (IPoint)context.createObject(Point.getClsid());;    
	            pPoint.setX(Double.parseDouble(pointArr[i]));
	            pPoint.setY(Double.parseDouble(pointArr[i+1]));
	            p.addPoint(pPoint, null, null);   
	            System.out.println(pointArr[i]);
	            System.out.println(pointArr[i+1]);
	       }
		  //strkeyValues的字符串格式为:列名1=值1,列名2=值2,.....
		  String []keyValues=strkeyValues.split(",");
		  for(String strkeyValue:keyValues){
			  String [] keyValue=strkeyValue.split("=");
			  feature.setValue(fields.findField( keyValue[0]),  keyValue[1]);
		  }
		  feature.setShapeByRef( p);
		  feature.store();  
		  context.releaseContext();
   	  return EditResult.Succeed;
     }
     catch(Exception ee)
     {
   	  ee.printStackTrace();
   	  if(context!=null)
   	  {
   	    context.releaseContext();
   	  }
   	  return EditResult.Failed;
     }
}
/**
 * 添加面的方法
 * @param pointListString
 * @param serverName
 * @param serverType
 * @param layerId
 * @param strkeyValues
 * @return
 * @throws AutomationException
 * @throws IOException
 */
public String  addPolygon(String pointListString,String serverName,String serverType,int  layerId,String strkeyValues) throws AutomationException,IOException
{
	 String[] pointArr=pointListString.split(",");
    IServerContext context = null;
 try
 {
	      context =ServerContextFactory.getContext(serverName, serverType);
		  MapServer mapServer = (MapServer) context.getServerObject();
		  ILayer editLayer = mapServer.getLayer("",layerId);
		  System.out.println("编辑的图层名称:"+editLayer.getName());
		  IFeatureLayer featureLayer = new IFeatureLayerProxy(editLayer);
		  IFeatureClass featureClass = featureLayer.getFeatureClass();
		  IFeature feature = featureClass.createFeature();
		  IFields fields = feature.getFields(); 
		  Polygon polygon=(Polygon)context.createObject(Polygon.getClsid());
	      for  ( int  i =  0 ; i < pointArr.length; i+=2)    
	      {    
	            IPoint pPoint;     
	            pPoint = (IPoint)context.createObject(Point.getClsid());;    
	            pPoint.setX(Double.parseDouble(pointArr[i]));
	            pPoint.setY(Double.parseDouble(pointArr[i+1]));
	            polygon.addPoint(pPoint, null, null);  
	       }
		  //strkeyValues的字符串格式为:列名1=值1,列名2=值2,.....
		  String []keyValues=strkeyValues.split(",");
		  for(String strkeyValue:keyValues){
			  String [] keyValue=strkeyValue.split("=");
			  feature.setValue(fields.findField( keyValue[0]),  keyValue[1]);
		  }
		  feature.setShapeByRef( polygon);
		  feature.store();  
		  context.releaseContext();
   	  return EditResult.Succeed;
     }
     catch(Exception ee)
     {
   	  ee.printStackTrace();
   	  if(context!=null)
   	  {
   	    context.releaseContext();
   	  }
   	  return EditResult.Failed;
     }
}
}
